<?php
/**
 * Created by PhpStorm.
 * User: gaojie
 * Date: 2018/11/16
 * Time: 10:09 AM
 */

namespace app\Http\Controllers\Bank;


use App\Http\Controllers\Controller;
use App\Model\StatisticsDeviceDayEnergy;
use Illuminate\Http\Request;
use App\Model\Site;


class BankController extends Controller
{

    public function __construct(){
        $this->middleware('token', ['except' => ['postBankForAuth']]);
    }
    public function postBankForAuth(Request $request)
    {
        $redis = $this->_Redis();
        $name = $request->input('name');
        $password = $request->input('password');
        $user = strtoupper($name);
        if($user != 'CIXIHUAXIABANK'){
            return $this->json_error('user error','40001');
        }
        //密码
//       $str =  str_random(32);
        if($password != 'GvejzPUjnvyPfZK' ){
            return $this->json_error('pd error','40001');
        }
        $tamp = date('Ymd');
//        $str = str_random(32);
//        dump($str);
        $salt = 'l7ExbAPH1vUFKbTDfpEv4tXWXlnErKu9';
        $str =  strtoupper($user.$tamp.$salt);
        $token = md5($str);
        $key = 'token:'.$user.':'.$token;
        $redis->set($key,'',3600);
        return $this->json_success($token);
    }


    public function getSiteList()
    {
       $lists =  Site::whereIn('status',[1,2])->get(['id','name']);
       $result = [];
       foreach ($lists as $list){
           $id = $list->id;
           $name = $list->name;
           array_push($result,['sid'=>$id,'sname'=>$name]);
       }
       if($result){
           return $this->json_success($result);
       }else{
           return $this->json_error('sid error','40003');
       }
    }

    public function getMonthGenOfSid(Request $request)
    {
        $list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13'
            , '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26',
            '27', '28', '29', '30', '31'];
        $year_list = ['2018','2019'];
        $sid = $request->input('sid');
        $month = $request->input('month');
        if(!$request->has('sid')){
            return $this->json_error(' sid error','40003');
        }
        if ($request->has('year')) {
            $year = $request->input('year');
        }else{
            $year = date('Y');
        }
        $sid = intval($sid);

        if(!is_numeric($sid)){
            return $this->json_error('sid error','40012');
        }
        $isin_month = in_array($month, $list);
        $isin_year = in_array($year,$year_list);
        if (!($isin_month &&is_string($month))) {
            return $this->json_error('wrong month value', '40004');
        }
        if(!($isin_year && is_string($year))){
            return $this->json_error('wrong year value', '40005');
        }
        if($month < 9){
            $month = '0'.$month;
        }
        $month_begin = $year.'-'.$month.'-01';
        if(date('Y')!=$year) {
            $month_end = $year . '-' . date('m-d', strtotime("$month_begin +1 month -1 day"));
        }else{
            $month_end = date('Y-m-d',strtotime('-1 day'));
        }
        $value =StatisticsDeviceDayEnergy::where('site_id',$sid)->where('date','<',$month_end)->where('date','>',$month_begin)->sum('value');
        if($value) {
            return $this->json_success($value);
        }else{
            if($value == 0){
                return $this->json_success($value/2);
            }else {
                return $this->json_error('sys error', '40015');
            }
        }

    }

    public function getYearGenOfSid(Request $request){

        $sid = $request->input('sid');
        $list = ['2018','2019'];
        $year = $request->input('year');
        if(!$request->has('sid')){
            return $this->json_error(' sid error','40003');
        }
//        $firstday = date('Y-m-01', time());
        $sid = intval($sid);
        if(!is_numeric($sid)){
            return $this->json_error('sid error','40003');
        }
        $isin = in_array($year,$list);
        if(!($isin&&is_string($year))){
            return $this->json_error(' Wrong year value','40005');
        }
        $year_begin = $year.'-'.'01-01';
        $year_end = date('Y-m-d', strtotime("$year_begin +1 year -1 day"));
        $value =StatisticsDeviceDayEnergy::where('site_id',$sid)->where('date','<',$year_end)->where('date','>',$year_begin)->sum('value');
        if($value) {
            return $this->json_success($value/2);
        }else{
            return $this->json_error('sys error','40015');
        }
    }
}